#include <myheader_c_plus.h>
using namespace std;
struct Record{
    string _word;
    int _num;
};
class Dictionary{
private:
    vector<Record> dic;
public:
    //构造函数，申请足够的空间
    Dictionary(){
        dic.reserve(sizeof(Record)*3000);
    }
    void read(const string &fileName){
        //打开文件
        ifstream ifs(fileName);
        string word;
        while(ifs >> word){
           // cout << 20 << " " << "word = " << word << endl;
            //判断这个字符是否在vector内
            auto it = dic.begin();
            while(it != dic.end()){
                if(it->_word == word){
                    it->_num++;
                    break;
                }
                it++;
            }

            if(it != dic.end()){
                continue;
            }
            //没有找到相同的
            Record rec;
            rec._word = word;
            rec._num = 1;
            dic.push_back(rec);
            //sleep(1);
            //for(long unsigned i = 0; i < dic.size(); i++){
              //  cout << 40 << "_word = " << dic[i]._word << "_num = " << dic[i]._num << endl;
            //}
          // cout << "==============" << endl;
        }
        ifs.close();
        //sleep(1);

    }
    void write(const string &fileName){
        ofstream ofs(fileName);
        //将dic里的结构体输入到文件
        auto it = dic.begin();
        while(it != dic.end()){
            ofs << it->_word << " " << it->_num << endl;
            it++;
        }
        ofs.close();

    }
};
int main(int argc, char *argv[]){
    Dictionary dic;
    auto start = std::chrono::high_resolution_clock::now();
    dic.read("The_Holy_Bible.txt");
    dic.write("dict.txt");
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
    cout << "runtime = " << duration.count() << " ms" <<  endl;
    return 0;
}

